package com.demo.druid.modules.system.user.controller;

import com.demo.druid.base.response.ResList;
import com.demo.druid.modules.system.user.entity.User;
import com.demo.druid.modules.system.user.service.impl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangjianhua
 * @Description controller返回值已经经过advice统一封装
 * @date 2021/9/1/001 14:04
 **/
@RestController
@RequestMapping("system/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * @param user 基本分页条件
     * @return 分页结果
     */
    @PostMapping(value = "list",produces = "application/json")
    public ResList<User> get(@RequestBody User user)
    {
        List<User> userList = userService.find(user);
        PageInfo pageInfo = new PageInfo(userList);
        return new ResList<User>(userList,(int)pageInfo.getTotal());
    }

    @GetMapping("{id}")
    public User get( @PathVariable int id)
    {
        return userService.findById(id);
    }

    @PostMapping
    public void save(@RequestBody User user){
        userService.save(user);
    }

    @DeleteMapping("{id}")
    public void deleteById( @PathVariable("id") int id){
        userService.deleteById(id);
    }
}
